﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WLYT.Application.Interfaces;
using WLYT.Domain.Model;
using WLYT.Repository;

namespace WLYT.Application.Services
{
    public class LogRecordService : BaseService<LogRecord>, ILogRecordService
    {
        public ISqlSugarRepository<LogRecord> LogRecordRepository;
        public LogRecordService(ISqlSugarRepository<LogRecord> logRecordRepository) : base(logRecordRepository)
        {
            LogRecordRepository = logRecordRepository;
        }

        public async Task<Tuple<IList<LogRecord>, int>> GetModelsByPageAsync(DateTime? time, string url, string content, int pageSize, int pageIndex)
        {
            Expressionable<LogRecord> exp = Expressionable.Create<LogRecord>();
            exp.AndIF(time != null, p => SqlFunc.Between(p.CreateTime, time, (time ?? DateTime.Now).AddDays(1)));
            exp.AndIF( !string.IsNullOrEmpty(url), p => p.RequestUri.Contains(url));
            exp.AndIF( !string.IsNullOrEmpty(content), p => p.RequestContent.Contains(content));
            return await GetPageListAsync(exp, pageSize, pageIndex, p => p.Id);
        }
    }
}
